<!--
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2024-06-04 22:12:02
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2024-06-29 00:14:34
 * @FilePath: /chat-ai-plus/src/components/ChatHistory/ChatHistory.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <scroll-view
    class="full"
    scroll-y="true"
    :scroll-top="scrollTop"
    upper-threshold="300"
  >
    <div class="chat-list p_sm">
      <div
        class="chat-item "
        v-for="item in instructionList"
        :key="item.id"
       
      >
       
        <div class="content flex t_line"  @click="handleClick(item)">
          {{ item.question }}
        </div>
        <u-icon class="icon icon-btn" name="edit-pen" @click="navigate(item)"></u-icon>
        <u-icon class="icon icon-btn ml_xs" name="trash" @click="handleRemove(item)"></u-icon>
      </div>
 
    </div>
  </scroll-view>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted, ref, watch } from 'vue';
import emitter from '@/utils/emitter';
import useChatStore from '@/store/modules/chat';
import './index.scss';
import useInstructionStore from '@/store/modules/instruction';

export default defineComponent({
  name: 'ToolSet',

  setup() {
    const chatStore = useChatStore();
    const instructionStore = useInstructionStore();
    const instructionList = computed(() => instructionStore.instructionList.sort((a,b) => {

      return b.id - a.id
    }))



    const scrollTop = ref(0);
    const handleClick = (item) => {
      chatStore.setSending(true);
      chatStore.clearConversations();
      chatStore.setCurrentRoom();
      chatStore.toggleModel('GPT-3.5');
  

      emitter.emit('send', item.question)
    }


    watch(() => chatStore.currentModel,(model) => {
      instructionStore.getInstructionList(model.version);
    })
    onMounted(() => {
      instructionStore.getInstructionList(chatStore.currentModel.version);
    });

    const navigate = (item) => {
      uni.navigateTo({
        url: `/pages/instruct/index?id=${item.id}`,
      });
    }

    const handleRemove = async (item) => {
      await instructionStore.deleteTopInstructionList(item.id);
      instructionStore.getInstructionList(chatStore.currentModel.version);
    }

    return {
      scrollTop,
      instructionList,
      handleClick,
      navigate,
      handleRemove
    };
  },
});
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>

